<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/9
 * Time: 10:38
 */

namespace app\applet\model;


use think\Db;

class Personal
{
    /**
     * 我的收藏列表
     * $type 1企业展厅,2人才展厅,3征婚,4短租员工1企业展厅,2人才展厅,3征婚,4短租员工
     *      5我要兼职,6兼职招募,7投资意向,8股东招募,9加盟代理,10联合办公
     *      11企业招募,12采购邀标
     */
    public function coList($member_id,$type,$page,$page_size){
        $filed='';
        $join_table='';
        $join_string='';
        if($type==13){
            $filed='c.c_id,c.object_id,sa.company_name,sa.create_time';
            $join_table='store_apply sa';
            $join_string='sa.sa_id=c.object_id';
        }
        $data=Db::name('collection')->alias('c')
            ->field($filed)
            ->join($join_table,$join_string)
            ->where(['c.member_id'=>$member_id,'c.class_type'=>$type])
            ->page($page,$page_size)
            ->select();
        //dump($data);
        if($type==13){
            foreach ($data as $k=>&$v){
                $pic=$this->selectStore($v['object_id']);
                $v['store_pic']=$pic;
            }
        }

        //核心优势  推介佣金  代理佣金  信誉保证金  都还没有

        $total_num = Db::name('collection')->alias('c')
            ->join($join_table,$join_string)
            ->where(['c.member_id'=>$member_id,'c.class_type'=>$type])
            ->count();
        if($total_num>0){
            $t_page=ceil($total_num / $page_size);
        }else{
            $t_page=0;
        }

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;

    }

    /**
     * 我的收藏页面
     */
    public function myCollection($member_id){
        $data=[];
        //联盟商家收藏数  13
        $store_count=Db::name('collection')->alias('c')
            //->join('member m','m.member_id=c.member_id')
            //->where(['c.object_id'=>$store_id,'c.class_type'=>13,'c.member_id'=>$member_id])->count();
            ->where(['c.class_type'=>13,'c.member_id'=>$member_id])->count();
        //$data['store_count']=$store_count;

        //人才展厅收藏
        $data['personal_count']=0;
        //企业展厅收藏
        $data['enterprise_count']=0;
        //征婚交友收藏
        $data['marriage_count']=0;
        //短租员工收藏
        $data['short_count']=0;
        //我要兼职收藏
        $data['staff_count']=0;
        //兼职招募收藏
        $data['recruit_count']=0;
        //投资意向收藏
        $data['invest_count']=0;
        //股东招募收藏
        $data['partner_count']=0;
        //加盟代理收藏
        $data['join_count']=0;
        //联合办公收藏
        $data['union_count']=0;
        //企业招聘收藏
        $data['business_count']=0;
        //采购邀标收藏
        $data['purchase_count']=0;

        return $data;
    }

    /**
     * 我的收藏---商品收藏列表
     */
    public function goodsList($member_id,$page,$page_size){
        $filed='c_id,object_id';
        $data=Db::name('collection')
            ->field($filed)
            ->where(['member_id'=>$member_id,'class_type'=>15])
            ->page($page,$page_size)
            ->select();

        foreach ($data as $k=>&$v){

            $goods=Db::name('goods')
                ->field('goods_id,g_name,cover_pic,g_type,source_type,store_id')
                ->where('goods_id',$v['object_id'])
                ->find();
            if(empty($goods)){
                $choice_goods=Db::name('choice_goods')
                    ->field('goods_id,title,cover,shop_type,source,brand_name,agreement_price,cate_id')
                    ->where('goods_id',$v['object_id'])
                    ->find();
               $v['goods_id']=$choice_goods['goods_id'];
               $v['goods_name']=$choice_goods['title'];
               $v['cover_pic']=$choice_goods['cover'];
               $v['price']=$choice_goods['agreement_price'];
               //所属商城
               if($choice_goods['shop_type']==1){
                   $v['shop_store_name']='返利商城';
                   $v['shop_type']=2;
               }else{
                   $v['shop_store_name']='爆品商城';
                   $v['shop_type']=3;
               }
                //商品品牌
                $v['store_brand_name']=$choice_goods['brand_name'];
                //一级 商品分类
                //$c_id=substr($choice_goods['cate_id'],strripos($choice_goods['cate_id'],",")+1);//二级分类
                $c_id=substr($choice_goods['cate_id'],strripos($choice_goods['cate_id'],",")-1);//一级分类
                //商品分类
                $cate2=Db::name('choice_cate')
                    ->field('name')
                    ->where(['id'=>$c_id])
                    ->find();
                $v['cate_name']=$cate2['name'];
                $v['g_type']=6;//供应链商品

            }else{
                $v['goods_id']=$goods['goods_id'];
                $v['goods_name']=$goods['g_name'];
                $v['cover_pic']=$goods['cover_pic'];
                $goods_spec=Db::name('goods_spec')
                    ->where('goods_id',$goods['goods_id'])
                    ->find();
                $v['price']=$goods_spec['price'];
                //所属商城
                if($goods['source_type']==1){
                    $v['shop_store_name']='联盟商城';
                    $v['shop_type']=1;
                }elseif ($goods['source_type']==2){
                    $v['shop_store_name']='返利商城';
                    $v['shop_type']=2;
                }else{
                    $v['shop_store_name']='爆品商城';
                    $v['shop_type']=3;
                }
                //店铺名称
                $store=Db::name('store_apply')->where('sa_id',$goods['store_id'])
                    ->find();
                if(empty($store)){
                    $v['store_brand_name']='';
                }else{
                    $v['store_brand_name']=$store['company_name'];
                }

                //商品类型
                if($goods['g_type']==1){
                    $v['cate_name']='消费爆品';
                    $v['g_type']=1;
                }elseif($goods['g_type']==2){
                    $v['cate_name']='裂变爆品';
                    $v['g_type']=2;
                }elseif($goods['g_type']==3){
                    $v['cate_name']='返利商品';
                    $v['g_type']=3;
                }elseif($goods['g_type']==4){
                    $v['cate_name']='限时特惠';
                    $v['g_type']=4;
                }else{
                    $v['cate_name']='商家讲坛';
                    $v['g_type']=5;
                }

            }

        }


        $total_num = Db::name('collection')
            ->where(['member_id'=>$member_id,'class_type'=>15])
            ->count();


        $list['current_page'] = $page;
        $list['total_page'] =ceil($total_num / $page_size);
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;
    }

    /**
     * 我的收藏---店铺收藏列表
     */
    public function colStoreList($member_id,$page,$page_size){
        $filed='c.c_id,c.object_id,sa.company_name,sa.ic_id,sa.introduce';
        $data=Db::name('collection')->alias('c')
            ->field($filed)
            ->join('store_apply sa','sa.sa_id=c.object_id')
            ->where(['c.member_id'=>$member_id,'c.class_type'=>13])
            ->page($page,$page_size)
            ->select();
        foreach ($data as $k=>&$v){
            //行业名称
            $cate=Db::name('industry_class')->where('ic_id',$v['ic_id'])
                ->find();
            $v['cate_name']=$cate['ic_name'];
            //店铺图片
            $store=Db::name('store_detail')->where('sa_id',$v['object_id'])
                ->find();
            $img_list = json_decode($store['img_list'],true);
            $v['cover_pic'] = $img_list[0];

            unset($v['ic_id']);
        }


        $total_num = Db::name('collection')->alias('c')
            ->join('store_apply sa','sa.sa_id=c.object_id')
            ->where(['c.member_id'=>$member_id,'c.class_type'=>13])
            ->count();


        $list['current_page'] = $page;
        $list['total_page'] =ceil($total_num / $page_size);
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;
    }

    /**
     * 删除收藏
     */
    public function collectionDel($member_id,$id){
        Db::name('collection')
            ->where(['member_id'=>$member_id,'c_id'=>$id])
            ->delete();

        return true;
    }

    /**
     * 查询店铺的轮播图的第一张
     */
    public function selectStore($store_id){
        $one=Db::name('store_detail')
            ->where('sa_id',$store_id)
            ->find();
        $img_list='';
        if(!empty($one)){
            $data = json_decode($one['img_list'],true);

            if(!empty($data)){
                foreach ($data as $k=>$v){
                    if($k==0){
                        $img_list=$v;
                    }
                }
            }

        }


        return $img_list;
    }

    /**
     * 干货收藏列表
     */
    public function videoList($member_id,$type,$page,$page_size){

        //商家名称  商品图片  课程标题  多少人观看
        //$type  1=视频,2=音频

        $data=Db::name('collection')->alias('c')
            ->field('c.c_id,g.goods_id,g.g_name,g.cover_pic,g.store_id,g.g_type')
            ->join('goods g','g.goods_id=c.object_id')
            ->where(['c.member_id'=>$member_id,'c.class_type'=>14,'c.is_type'=>$type])
            ->page($page,$page_size)
            ->select();

        foreach ($data as $k=>&$v){
            //观看人数统计

            $count=Db::name('course_viewers_record')
                ->where('goods_id',$v['goods_id'])
                ->count();
            $v['viewers_num']=$count;
            //店铺名称
            if($v['store_id']>0){
                $v['company_name']=Db::name('store_apply')
                    ->where('sa_id',$v['store_id'])->value('company_name');
            }else{
                $v['company_name']='自营店铺';
            }

        }

        $count= Db::name('collection')->alias('c')
            ->join('goods g','g.goods_id=c.object_id')
            ->where(['c.member_id'=>$member_id,'c.class_type'=>14,'c.is_type'=>$type])
            ->count();

        $list['current_page'] = $page;
        $list['total_page'] =ceil($count / $page_size);
        $list['page_num'] = $page_size;
        $list['total_num'] = $count;
        $list['data'] = $data;

        return $list;
    }

    /**
     * 我的粉丝--页面
     */
    public function myFans($store_id,$member_id){
        $data=[];
        //联盟商家粉丝数  13
        $store_count=Db::name('collection')->alias('c')
            ->join('member m','m.member_id=c.member_id')
            ->where(['c.object_id'=>$store_id,'c.class_type'=>13])->count();
        //dump(Db::name('collection')->getLastSql());
        $data['store_count']=$store_count;
        //人才展厅粉丝
        $data['personal_count']=0;
        //企业展厅粉丝
        $data['enterprise_count']=0;
        //征婚交友粉丝
        $data['marriage_count']=0;
        //短租员工粉丝
        $data['short_count']=0;
        //我要兼职粉丝
        $data['staff_count']=0;
        //兼职招募粉丝
        $data['recruit_count']=0;
        //投资意向粉丝
        $data['invest_count']=0;
        //股东招募粉丝
        $data['partner_count']=0;
        //加盟代理粉丝
        $data['join_count']=0;
        //联合办公粉丝
        $data['union_count']=0;
        //企业招聘粉丝
        $data['business_count']=0;
        //采购邀标粉丝
        $data['purchase_count']=0;

        return $data;
    }

    /**
     * 联盟商家粉丝
     */
    public function storeList($store_id,$page,$page_size){
        $data=Db::name('collection')->alias('c')
            ->field('c.c_id,c.member_id,m.nickName,m.avatarUrl')
            ->join('member m','m.member_id=c.member_id')
            ->where(['c.object_id'=>$store_id,'c.class_type'=>13])
            ->page($page,$page_size)
            ->select();

        if(!empty($data)){

            foreach ($data as $k=>&$v){
                //用户公司名称，职位，行业，个人特点，是否实名认证
                $m_info=Db::name("member_info")
                    ->where(['member_id'=>$v['member_id']])
                    ->find();
                if(!empty($m_info)){
                    $v['company']=$m_info['company'];
                    $v['com_post']=$m_info['com_post'];
                    $v['industry']=$m_info['industry'];
                    $v['signature']=$m_info['signature'];
                    $v['address']=$m_info['address'];

                    if($m_info['status']==1){
                        $v['is_real']=1;//是
                    }else{
                        $v['is_real']=2;//否
                    }

                }else{
                    $v['company']='';
                    $v['com_post']='';
                    $v['industry']='';
                    $v['signature']='';
                    $v['address']='';
                    $v['is_real']=2;//否
                }
                //是否股东认证
                $info=Db::name("partner_certification")
                    ->where(['member_id'=>$v['member_id'],'status'=>1])
                    ->find();
                if(!empty($info)){
                    $v['is_partner']=1;//是
                }else{
                    $v['is_partner']=2;//否
                }
                //用户信誉保证金
                $v['credit_deposit']=Db::name('member_asset')
                    ->where('member_id',$v['member_id'])
                    ->value('credit_ensure');

            }
        }

        $count= Db::name('collection')->alias('c')
            ->join('member m','m.member_id=c.member_id')
            ->where(['c.object_id'=>$store_id,'c.class_type'=>13])
            ->count();

        $list['current_page'] = $page;
        $list['total_page'] =ceil($count / $page_size);
        $list['page_num'] = $page_size;
        $list['total_num'] = $count;
        $list['data'] = $data;

        return $list;
    }

    /**
     * 查询商家id
     */
    public function selectStoreId($member_id){
        $store=Db::name('store_apply')
            ->where(['member_id'=>$member_id,'status'=>1])
            ->find();
        if(empty($store)){
            $store_id=0;
        }else{
            $store_id=$store['sa_id'];
        }
        return $store_id;
    }

    /**
     * 免费群发消息-----站内信
     */
    public function sendMessage($param,$member_id){

        $data['create_time']=time();
        $data['member_id']=$member_id;
        $data['title']='消息提醒';
        $data['content']=$param['content'];
        $data['object_type']=2;
        $data['object_arr']=$param['object_id'];
        $data['class_type']=$param['class_type'];

        Db::name('message')->insert($data);

        return true;
    }
}